﻿using AutoSelfApp.Common;
using AutoSelfApp.Entity;
using DevExpress.Utils;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;

namespace AutoSelfApp.Views
{
    public delegate void BackHomeEvent();
    public partial class PriceQueryPage : DevExpress.XtraEditors.XtraUserControl,IPageCtrl
    {
        private List<GridColumnInfo> columnInfos = new List<GridColumnInfo>();
        private List<GridColumnInfo> columnInfos1 = new List<GridColumnInfo>();
        public BackHomeEvent backHome;
        public String type = "";
        private FrmKeyBoard frmKeyBoard = new FrmKeyBoard();
        public int page = 1;
        private IniFileUtil iniFile = new IniFileUtil(Path.Combine(DirectoryUtil.GetCurrentDirectory(), "application.ini"));

        public PriceQueryPage(SampleDataItem item)
        {
            if (item.Title == "物价查询")
            {
                type = "1";
            }
            else
            {
                type = "2";
            }
            InitializeComponent();
            frmKeyBoard.keyBoardPress += (p) =>
            {
                showLetter(p);
            };
        }

        private void InitForm()
        {
            #region
            columnInfos.Add(new GridColumnInfo("SHOUFEIXMMC", "收费项目名称", 100));
            columnInfos.Add(new GridColumnInfo("JIJIADW", "单位", 100));
            columnInfos.Add(new GridColumnInfo("DANJIA1", "价格", 100)
            {
                Formattype = FormatType.Numeric,
                Formatstring = "{0:n2}"
            });
            columnInfos1.Add(new GridColumnInfo("SHOUFEIXMMC", "药品项目名称", 300));
            columnInfos1.Add(new GridColumnInfo("CHANDIMC", "产地", 200));
            columnInfos1.Add(new GridColumnInfo("JIJIADW", "规格", 100));
            columnInfos1.Add(new GridColumnInfo("DANJIA1", "价格", 100)
            {
                Formattype = FormatType.Numeric,
                Formatstring = "{0:n2}"
            });
            #endregion
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lblTime.Text = PriceConstants.time.ToString();
            if (PriceConstants.time == 0)
            {
                frmKeyBoard.Hide();
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            page = 1;
            SelectPrice(page.ToString(), type);
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            ClearScreen();
        }

        private void upButton_Click(object sender, EventArgs e)
        {
            page--;
            SelectPrice(page.ToString(), type);
        }

        private void downButton_Click(object sender, EventArgs e)
        {
            page++;
            SelectPrice(page.ToString(), type);
        }


        private void SelectPrice(String pageNum,String type)
        {
            InitForm();
            QueryPriceParam queryPrice = new QueryPriceParam();
            queryPrice.xiangmumc = textEdit1.Text;
            queryPrice.type = type;
            queryPrice.pageNum = pageNum;
            queryPrice.pageSize = iniFile.IniReadValue("Application", "pageSize");
            var stream = JsonConvert.DeserializeObject<QueryPriceDto>(PostQueryPrice(queryPrice));
            List<PriceList> price = stream.rows;
            var page = stream.total/12+1;
            pageMessage.Text = "当前为第"+ pageNum + "页，一共有" + page + "页";
            if (pageNum == "1")
            {
                upButton.Enabled = false;
            }
            else {
                upButton.Enabled = true;
            }
            if (pageNum == page.ToString())
            {
                downButton.Enabled = false;
            }
            else {
                downButton.Enabled = true;
            }
            gcData.DataSource = price;
            if (type == "1")
            {
                DevPublic.SetGridControlView(gvData, columnInfos, true, true);
            }
            else if(type == "2")
            {
                DevPublic.SetGridControlView(gvData, columnInfos1, true, true);
            }
            
        }

        //Post请求
        private String PostQueryPrice(QueryPriceParam queryPrice)
        {
            var client = new RestClient("http://192.168.0.161:8082/hospapi/webchat/itemprice/list");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AlwaysMultipartFormData = true;
            request.AddParameter("shoufeixmmc", queryPrice.xiangmumc);
            request.AddParameter("type", queryPrice.type);
            request.AddParameter("pageNum", queryPrice.pageNum);
            request.AddParameter("pageSize", queryPrice.pageSize);
            IRestResponse response = client.Execute(request);
            return response.Content;
        }

        public void showLetter(string Letter)
        {
            if (textEdit1.Text.Length == 0 && Letter == "DEL") {
                return;
            }
            if (Letter == "DEL")
            {
                textEdit1.Text = textEdit1.Text.Substring(0, textEdit1.Text.Length - 1);
            }
            else if (Letter == "QUERY") {
                frmKeyBoard.Hide();
                page = 1;
                SelectPrice(page.ToString(), type);
            }
            else
            {
                textEdit1.Text = textEdit1.Text + Letter;
            }
            
        }
        private void SetFocus()
        {
            textEdit1.Focus();
            textEdit1.SelectionStart = textEdit1.Text.Length;
        }

        public void StartTime()
        {
            timer1.Enabled = true;
            timer1.Start();
        }


        private void textEdit1_Click(object sender, EventArgs e)
        {
            showKeyBoard();
        }

        private void showKeyBoard()
        {
            frmKeyBoard.Show();
            frmKeyBoard.Top = int.Parse(iniFile.IniReadValue("Application", "top"));
            frmKeyBoard.Left = int.Parse(iniFile.IniReadValue("Application", "left"));
            frmKeyBoard.BringToFront();
        }

        public void ClearScreen()
        {
            textEdit1.Text = "";
            page = 1;
            SelectPrice(page.ToString(), type);
        }
    }
}